package com.qk.management.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/role/{roleLabel}")
    public Result selectByLabel(@PathVariable String roleLabel) {
        List<User> users=userService.selectByLabel(roleLabel);
        return Result.success(users);
    }

    @PutMapping
    public Result update(@RequestBody User user){

       userService.update(user);
        return Result.success();

    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        User user=  userService.selectById(id);
        return Result.success(user);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        userService.deleteByIds(ids);
        return Result.success();
    }
@PostMapping
public Result add(@RequestBody User user){
    userService.add(user);
    return Result.success();
}

    @GetMapping
    public Result list(UserDTO dto) {
        PageResult<UserDO> pageResult= userService.list(dto);
        return Result.success(pageResult);
    }
    @GetMapping("/list")
    public Result listAll(){
       List<User> list= userService.listAll();
       return Result.success(list);
    }

}
